
using System;
using Microsoft.Xna.Framework;


namespace Tama_Chan.Utils
{
   public static class TransformUtils
   {

      public static float GetAngleBetweenPositions(Vector2 originPosition, Vector2 orbiterPosition)
      {
         return (float)Math.Atan2(orbiterPosition.X - originPosition.X, orbiterPosition.Y - originPosition.Y);
      }


      public static Vector2 GetRotatedPosition(ref float thisAngle, Vector2 thisOrigin, Vector2 thisPosition, float thisSpeed, bool clockWise)
      {
         return GetRotatedPosition(ref thisAngle, Vector2.Distance(thisOrigin, thisPosition), thisOrigin, thisSpeed, clockWise);
      }


      public static Vector2 GetRotatedPosition(ref float thisAngle, float thisDistance, Vector2 thisOrigin, float thisSpeed, bool clockWise)
      {
         /*
          * Radians in a circle = 2pi * r. Since it's pixels, 2pi * r = pixels in circumference. So, speed in pixels / circumference
          * in pixels gives radians, which is just a representation of a fraction of the circumference. Because the distance and speed
          * are both in pixels it works out to radians.
          */

         if(clockWise)
         {
            thisSpeed *= -1;
         }

         thisAngle += thisSpeed / (MathHelper.TwoPi * thisDistance);
         thisAngle = MathHelper.WrapAngle(thisAngle);

         return new Vector2(thisOrigin.X + thisDistance * (float)Math.Sin(thisAngle), thisOrigin.Y + thisDistance * (float)Math.Cos(thisAngle));
      }
      
      
      /// <summary>
      /// Updates the input scale. Scales are percents of original sprite size, so 0.5 would be 50% and 5.0 would be 500%.
      /// </summary>
      public static bool GetScale(ref float currentScale, GameTime gameTime, float deltaPerSecond, bool isExpanding, float limit)
      {
         currentScale += (gameTime.ElapsedGameTime.Ticks / (float)TimeSpan.TicksPerSecond * deltaPerSecond);

         bool limitReached = false;
         if(isExpanding && currentScale > limit || !isExpanding && currentScale < limit)
         {
            currentScale = limit;
            limitReached = true;
         }

         return limitReached;
      }


      public static Vector2 GetPerpendicular(Vector2 heading, bool clockwise)
      {
         if(clockwise)
         {
            return new Vector2(-heading.Y, heading.X);
         }

         return new Vector2(heading.Y, -heading.X);
      }

   }
}
